﻿using System;
using Ext.Net;

namespace ERP.Modules
{
    public partial class MCK_Holiday : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                GetData();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }

        protected void StoreMCK_Holiday_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetData();
        }

        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            WindowsMCK_Holiday.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grMCK_Holiday.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    ClearTextBox();
                    DataDetail(int.Parse(row.RecordID));
                    WindowsMCK_Holiday.Show();
                    sm.UpdateSelection();
                }
        }

        protected void MCK_Holiday_DblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int Holiday_ID = int.Parse(e.ExtraParams["Holiday_ID"]);
            DataDetail(Holiday_ID);
            WindowsMCK_Holiday.Show();
        }

        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtHoliday_ID.Text == string.Empty)
            {
                try
                {
                    var obj = new ET.MCK_Holiday_ET
                    {
                        Holiday_Name = txtHoliday_Name.Text,
                        Start_Time = DateTime.Parse(txtStart_Time.Text),
                        End_Time = DateTime.Parse(txtStart_Time.Text).AddDays(Double.Parse(txtEnd_Time.Text) - 1)
                    };
                    bool Save = BLL.MCK_Holiday_BLL.Insert_MCK_Holiday(obj);
                    if (Save)
                    {
                        for (int i = 0; i < int.Parse(txtEnd_Time.Text); i++)
                        {
                            BLL.MCK_Holiday_BLL.InsertMCK_Holiday_Detail(obj.Holiday_ID, DateTime.Parse(txtStart_Time.Text).AddDays(i));
                        }
                        HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    }
                    GetData();
                    ClearTextBox();
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
                }
            }
            else
            {
                try
                {
                    var obj = new ET.MCK_Holiday_ET
                    {
                        Holiday_ID = int.Parse(txtHoliday_ID.Text),
                        Holiday_Name = txtHoliday_Name.Text,
                        Start_Time = DateTime.Parse(txtStart_Time.Text),
                        End_Time = DateTime.Parse(txtStart_Time.Text).AddDays(Double.Parse(txtEnd_Time.Text))
                    };
                    bool save = BLL.MCK_Holiday_BLL.Update_MCK_Holiday(obj);
                    if (save)
                    {
                        BLL.MCK_Holiday_BLL.DeleteMCK_Holiday_Detail(int.Parse(txtHoliday_ID.Text));
                        for (int i = 0; i < int.Parse(txtEnd_Time.Text); i++)
                        {
                            BLL.MCK_Holiday_BLL.InsertMCK_Holiday_Detail(int.Parse(txtHoliday_ID.Text), DateTime.Parse(txtStart_Time.Text).AddDays(i));
                        }
                        HTML.clsFormat.Alert_INFO("Thông Báo", "Sửa thành công !");
                    }
                    GetData();
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
                }
            }
        }

        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grMCK_Holiday.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    try
                    {
                        BLL.MCK_Holiday_BLL.DeleteMCK_Holiday_Detail(int.Parse(row.RecordID));
                        BLL.MCK_Holiday_BLL.Delete_MCK_Holiday(int.Parse(row.RecordID));
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetData();
        }
        #region Get
        private void GetData()
        {
            StoreMCK_Holiday.DataSource = BLL.MCK_Holiday_BLL.GetData_MCK_Holiday(string.Empty);
            StoreMCK_Holiday.DataBind();
        }
        private void DataDetail(int id)
        {
            var dt = BLL.MCK_Holiday_BLL.GetDataRow_MCK_HolidayDetail(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                txtHoliday_ID.Text = dt.Rows[0]["Holiday_ID"].ToString();
                txtHoliday_Name.Text = dt.Rows[0]["Holiday_Name"].ToString();
                txtStart_Time.Text = dt.Rows[0]["Start_Time"].ToString();
                txtEnd_Time.Text = dt.Rows[0]["WorkDays"].ToString();
            }
        }
        private void ClearTextBox()
        {
            FormPanelMCK_Holiday.Reset();

            txtHoliday_ID.Text = string.Empty;
        }
        #endregion
    }
}